package cn.flying.cloud.system.view;


import javax.annotation.Resource;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.flying.cloud.base.common.vo.Rt;
import cn.flying.cloud.base.common.vo.layui.TreeTableInfoVo;
import cn.flying.cloud.base.common.vo.layui.XmSelect;
import cn.flying.cloud.base.common.vo.layui.ZTreeInfo;
import cn.flying.cloud.system.common.service.CodeService;
import cn.flying.cloud.system.common.vo.ResTreeTableInfo;
import cn.flying.cloud.system.resource.entity.SysResInfo;
import cn.flying.cloud.system.resource.service.SysResInfoService;
import cn.flying.cloud.system.service.SysResService;

/**
 * @description: 权限资源
 * @author: admin
 * @date: 2022年05月22日 17:58
 * @version: 1.0
 */
@Controller
@RequestMapping("/sys/resource/")
public class SysResourceViewController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    private SysResInfoService sysResInfoService;
    @Resource
    private SysResService sysResService;
    @Resource
    private CodeService codeService;

    /**
     * 列表页面
     *
     * @return
     */
    @RequestMapping("index")
    public String list(Model model) {
        Map<String, String> statusList = codeService.getCodeList("status");
        model.addAttribute("statusList", statusList);
        return "sys/resource/index";
    }

    /**
     * 加载资源treeTable
     *
     * @return
     */
    @RequestMapping(value = "getTreeTableList", method = RequestMethod.POST)
    @ResponseBody
    public TreeTableInfoVo getResourceTreeTableList(SysResInfo resInfo) {
        List<ResTreeTableInfo> list = sysResService.getTreeTableList(resInfo);
        return new TreeTableInfoVo(0, "成功", list.size(), list);
    }


    /**
     * xm-select 资源下拉框数据
     *
     * @return
     */
    @PostMapping(value = "getSelectList")
    @ResponseBody
    public Rt<List<XmSelect>> getSelectList() {
        List<XmSelect> treeList = sysResService.getResourceSelectList();
        return Rt.success(treeList);
    }

    /**
     * 加载菜单树（角色关联资源）
     *
     * @return
     */
    @RequestMapping("getResourceTree")
    @ResponseBody
    public Rt<List<ZTreeInfo>> getResourceTree() {
        List<ZTreeInfo> permissionTrees = sysResService.getResourceTree();
        return Rt.success(permissionTrees);
    }

    /**
     * 新增/修改页面
     *
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("turn/edit")
    public String turnEdit(Long id, String resId, Model model) {
        Map<String, String> resTypes = codeService.getCodeList("res_type");
        model.addAttribute("resTypes", resTypes);
        if (id != null) {
            SysResInfo resInfo = sysResInfoService.getById(id);
            model.addAttribute("resource", resInfo);
        } else {
            SysResInfo resInfo = new SysResInfo();
            resInfo.setParentId(resId);
            model.addAttribute("resource", resInfo);
        }
        return "sys/resource/edit";
    }
//
//    /**
//     * 加载资源url列表数据
//     *
//     * @return
//     */
//    @RequestMapping(value = "getResourcePageUrlList", method = RequestMethod.POST)
//    @ResponseBody
//    public Rt getResourcePageUrlList(String resId) {
//        List<CmResourceUrl> list = sysResInfoService.getResourcePageUrlList(resId);
//        return Rt.success(list);
//    }
}
